const express = require('express');
const pool = require('../../pool.js');
const user = express.Router();
// 登录信息核对
user.post('/login', (req, res) => {
    pool.query('select * from user where user=? and pwd=? and roles=?', [req.body.user, req.body.pwd, req.body.roles], (err, r) => {
        if (err) {
            throw err;
        }
        res.send(r);
    })
})
user.post('/register', (req, res) => {
    let { user, pwd, email, user_name } = req.body;
    let roles = '商家';
    pool.query('insert user set user=?,pwd=?,email=?,roles=?,user_name=?', [user, pwd, email, roles, user_name], (err, r) => {
        if (err) {
            res.send({ msg: '该账号已被注册' });
        }
        res.send(r);
    })
})
user.put('/repwd', (req, res) => {
    let pwd = req.body.pwd;
    pool.query('update user set pwd=?', [pwd], (err, r) => {
        if (err) {
            throw err;
        }
        res.send(r);
    })
})
// 订单详情页显示用户信息，可以同时当作用户信息管理接口
user.get('/user', (req, res) => {
    let id = req.query.id;
    if (id) {
        pool.query('select * from user where id = ?', [id], (err, r) => {
            if (err) {
                throw err;
            }
            res.send(r);
        })
    } else {
        pool.query('select * from user', (err, r) => {
            if (err) {
                throw err;
            }
            res.send(r);
        })
    }
})

module.exports = user;